package controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

import pojo.Dept;
import pojo.Page;
import pojo.UserInfo;

import service.DeptService;
import service.UserInfoService;

@Controller
public class UserInfoController {

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private DeptService deptService;

	@RequestMapping(value = "login",produces = "application/json;charset=utf-8")
	public String login(String userAccount, String password,HttpServletResponse resp) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userAccount", userAccount);
		map.put("password", password);
		if (userAccount != null && password != null) {
			UserInfo login = userInfoService.login(map);
			if (login != null) {
				return "redirect:list";
			}
		}
		
		return "index";
	}
	
//	/^\w+@\w{2,3}.\w{2,3}$/
	@RequestMapping(value = "cheName", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String cheName(String userAccount) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userAccount", userAccount);
		UserInfo login = userInfoService.login(map);
		if (login == null) {
			return JSON.toJSONString("false");
		}
		return JSON.toJSONString(login.getPath());
	}
	

	@RequestMapping(value = "list", produces = "application/json;charset=utf-8")
	public String list(String userAccount,String deptId,@RequestParam(defaultValue="1") int pageNo,
			HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userAccount", userAccount);
		map.put("deptId", deptId);
		int count = userInfoService.getCount(map);
		
		Page page = new Page(pageNo, 2, count);
		map.put("pageNo", (pageNo-1)*2);
		map.put("pageSize",2);
		List<UserInfo> queryByPage = userInfoService.queryByPage(map);
		
		page.setList(queryByPage);
		List<Dept> allDepts = deptService.getAllDepts();
		req.setAttribute("page", page);
		req.getSession().setAttribute("allDepts", allDepts);
		req.setAttribute("userAccount", userAccount);
		req.setAttribute("deptId", deptId);
		req.setAttribute("pageNo", pageNo);
		return "list";
	}

	@RequestMapping(value = "toAdd", produces = "application/json;charset=utf-8")
	public String toAdd(HttpServletRequest req){
		List<Dept> allDepts = deptService.getAllDepts();
		req.getSession().setAttribute("allDepts", allDepts);
		return "add";
	}
	@RequestMapping(value = "add", produces = "application/json;charset=utf-8",method=RequestMethod.POST)
	public String add(UserInfo userInfo, HttpServletRequest req,
			@RequestParam(required = false) MultipartFile mu) throws Exception {
		if (!mu.isEmpty()) {
			String name = mu.getOriginalFilename();
			File file = new File(req.getSession().getServletContext()
					.getRealPath("/upload"), name);
			mu.transferTo(file);
			userInfo.setPath("upload/"+name);
			
		}
		userInfo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		boolean add = userInfoService.add(userInfo);
		if (add) {
			return "index";
		}
		return "add";
	}
	
	@RequestMapping(value = "del", produces = "application/json;charset=utf-8")
	public String del(int userId){
		boolean del = userInfoService.del(userId);
		if (del) {
			return "redirect:list";
		}
		return "redirect:list";
	}
	
	@RequestMapping(value = "toupdate", produces = "application/json;charset=utf-8")
	public String toupdate(int userId,HttpServletRequest req){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		UserInfo info = userInfoService.login(map);
		if (info!=null) {
			req.setAttribute("info", info);
			return "update";
		}
		return "redirect:list";
	}
	@RequestMapping(value = "update", produces = "application/json;charset=utf-8")
	public String update(UserInfo userInfo){
		boolean update = userInfoService.update(userInfo);
		if (update) {
			return "redirect:list";
		}
		return "update";
	}

}
